package com.dong.cloud.controller;

import com.dong.cloud.feign.PaymentFeign;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author DJ
 * @description
 * @date 2022/1/15 11:03 下午
 **/
@RestController
@Slf4j
// 这里可以添加 commandProperties = {},参数
@DefaultProperties(defaultFallback = "paymentGlobalFeignFallbackMethod")
public class OrderHystrixController {

    @Resource
    PaymentFeign paymentFeign;

    @HystrixCommand // 此注解如果不添加在对应的方法上则该方法不涉及服务降级
    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    public String paymentInfoOK(@PathVariable("id") Integer id) {
        return paymentFeign.paymentInfoOK(id);
    }

    /*@HystrixCommand(fallbackMethod = "paymentInfoTimeoutHandler", commandProperties = {
            // 设置属性参数 N s未返回就执行降级操作
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1500")
    })*/
    @HystrixCommand // 默认时间是1s 可以在application.yml中配置全局的超时时间，这里也可以自定义此接口的超时时间
    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    public String paymentInfoTimeout(@PathVariable("id") Integer id) {
        System.out.println("开始时间：" + LocalDateTime.now());
        String result = paymentFeign.paymentInfoTimeout(id);
        return result;
    }

    public String paymentInfoTimeoutHandler(Integer id) {
        return "我是消费者80，对方8001服务系统繁忙或出现异常请10秒钟后重试或自身出现异常请检查自己, id" + id + "o(╥﹏╥)o";
    }

    /**
     * 全局的服务降级方法
     *
     * @param
     * @return java.lang.String
     * @author DJ
     * @date 3:03 下午 2022/1/16
     **/
    public String paymentGlobalFeignFallbackMethod() {
        return "Global 异常处理信息，请稍后重试, o(╥﹏╥)o";
    }
}
